﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web;

namespace SimplePaginationDemo
{
    public class pagination
    {
        public  static string  GetPage(int page,int pagesize,int count){//当前页，分页记录数，总计数
            
           Uri url =HttpContext.Current.Request.Url;
           string purl = url.Segments[url.Segments.Length - 1];//获取当前地址
           StringBuilder sb = new StringBuilder();
           sb.Append(@"<div id=""dpage"">");

            //对于分页考虑两个端点的条件，即上一页和下一页的条件
           sb.Append(string.Format(@"<div><a href=""{0}?page={1}"">首页</div>",purl,1));
           if (page == 1)
           {
               sb.Append(@"<div class=""ddisable""><a href=""javascript:;"">上一页</div>");
           }
           else
           {
               sb.Append(string.Format(@"<div ><a href=""{0}?page={1}"">上一页</div>", purl, page - 1));
           }
           int pagecount = 0;
           if (count% pagesize == 0)
           {
               pagecount = (count) / pagesize;
           }
           else
           {
               pagecount =Convert.ToInt32( Math.Ceiling(Convert.ToDouble(count) / Convert.ToDouble(pagesize)));
           }
           if (page == pagecount)
           {
               sb.Append(@"<div class=""ddisable""><a href=""javascript:;"">下一页</div>");
           }
           else
           {
               sb.Append(string.Format(@"<div><a href=""{0}?page={1}"">下一页</div>", purl, page + 1));
           }
           sb.Append(string.Format(@"<div ><a href=""{0}?page={1}"">最后一页</div>", purl, pagecount));
           sb.Append(string.Format(@"<div ><a href=""javascript:;"">{0}/{1}</a></div>", page, pagecount));
           sb.Append("</div>");
           return sb.ToString();
        }
    }
}